//
//  YblbCell.swift
//  Auscultation
//
//  Created by 来 on 2020/12/9.
//

import UIKit

class YblbCell: UICollectionViewCell {
    
    var tzzx: UILabel!
    var status: UILabel!
    
    var url: String!
    var allTime: String = "00:00"
    
    // 播放
    var play_button: UIButton!
    // 时间
    var play_time: UILabel!
    // 进度
    var play_line: UIProgressView!
    
    var player: LGFAudioPlayer!
    
    deinit {
        debugPrint("LGFAudioPlayer 已释放")
        player?.stop()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let back_view = UIView.init()
        back_view.backgroundColor = UIColor.white
        back_view.clipsToBounds = true
        back_view.layer.cornerRadius = 4
        back_view.layer.borderColor = UIColor.init(lgf_HexString: "E1E4E7")?.cgColor
        back_view.layer.borderWidth = 0.5
        addSubview(back_view)
        back_view.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(-8)
        }
        
        tzzx = UILabel.init()
        tzzx.lgf_AddShowAllText()
        tzzx.text = "停诊中心-001"
        tzzx.font = UIFont.systemFont(ofSize: 16)
        tzzx.textColor = UIColor.init(lgf_HexString: "222222")
        back_view.addSubview(tzzx)
        tzzx.snp.makeConstraints { (make) in
            make.top.equalTo(25)
            make.left.equalTo(17)
        }
        
//        mz = UILabel.init()
//        mz.text = "来国锋"
//        mz.font = UIFont.systemFont(ofSize: 12)
//        mz.textColor = UIColor.init(lgf_HexString: "8D969D")
//        back_view.addSubview(mz)
//        mz.snp.makeConstraints { (make) in
//            make.top.equalTo(tzzx.snp.bottom).offset(15)
//            make.left.equalTo(tzzx)
//        }
        
//        let image_view = UIImageView.init(image: #imageLiteral(resourceName: "shijian@1.5x"))
//        back_view.addSubview(image_view)
//        image_view.snp.makeConstraints { (make) in
//            make.centerY.equalTo(mz)
//            make.left.equalTo(mz.snp.right).offset(13)
//            make.width.height.equalTo(12)
//        }

//        sj = UILabel.init()
//        sj.text = "2020.06.01-2020.06.1"
//        sj.font = UIFont.systemFont(ofSize: 12)
//        sj.textColor = UIColor.init(lgf_HexString: "8D969D")
//        back_view.addSubview(sj)
//        sj.snp.makeConstraints { (make) in
//            make.centerY.equalTo(mz)
//            make.left.equalTo(image_view.snp.right).offset(7)
//        }
        
//        status = UILabel.init()
//        status.text = "标注开始"
//        status.textAlignment = .center
//        status.backgroundColor = UIColor.init(lgf_HexString: "FFC28A")?.withAlphaComponent(0.1)
//        status.font = UIFont.systemFont(ofSize: 11)
//        status.textColor = UIColor.init(lgf_HexString: "F27C0F")
//        status.layer.borderColor = UIColor.init(lgf_HexString: "FFC28A")?.cgColor
//        status.layer.borderWidth = 1
//        back_view.addSubview(status)
//        status.snp.makeConstraints { (make) in
//            make.centerY.equalTo(tzzx)
//            make.right.equalToSuperview().offset(-17)
//            make.width.equalTo(55)
//            make.height.equalTo(19)
//        }
        
        // 播放
        play_button = UIButton.init(type: .custom)
        play_button.lgf_ViewName = "暂停"
        play_button.setBackgroundImage(#imageLiteral(resourceName: "bofang@1.5x"), for: .normal)
        play_button.addTarget(self, action: #selector(play), for: .touchUpInside)
        back_view.addSubview(play_button)
        play_button.snp.makeConstraints { (make) in
            make.top.equalTo(tzzx.snp.bottom).offset(12)
            make.left.equalTo(tzzx)
            make.width.height.equalTo(25)
        }
        
        play_time = UILabel.init()
        play_time.text = "00:00/00:00"
        play_time.font = UIFont.systemFont(ofSize: 12)
        play_time.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        back_view.addSubview(play_time)
        play_time.snp.makeConstraints { (make) in
            make.centerY.equalTo(play_button)
            make.right.equalToSuperview().offset(-15)
            make.width.equalTo(80)
        }
        
        play_line = UIProgressView.init()
        play_line.progress = 0.0
        play_line.progressTintColor = UIColor.init(lgf_HexString: "0F88F2")
        play_line.trackTintColor = UIColor.init(lgf_HexString: "CFD4D9")
        back_view.addSubview(play_line)
        play_line.snp.makeConstraints { (make) in
            make.centerY.equalTo(play_button)
            make.left.equalTo(play_button.snp.right).offset(10)
            make.right.equalTo(play_time.snp.left).offset(-10)
            make.height.equalTo(1)
        }
        
    }
    
    func initPlay() {
        if player == nil {
            NotificationCenter.default.addObserver(self, selector: #selector(CellPlayerStop(_:)), name: NSNotification.Name(rawValue: "YblbCellPlayerStop"), object: nil)
            player = LGFAudioPlayer()
            player.delegate = self
        }
    }
    
    @objc func play() {
        if play_button.lgf_ViewName == "暂停" {
            play_button.lgf_ViewName = "恢复"
            play_button.setBackgroundImage(#imageLiteral(resourceName: "暂停@1.5x"), for: .normal)
        } else {
            play_button.lgf_ViewName = "暂停"
            play_button.setBackgroundImage(#imageLiteral(resourceName: "播放@1.5x"), for: .normal)
        }
        if player.state == .playing {
            player.paused()
        } else if player.state == .paused {
            player.resume()
        } else {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YblbCellPlayerStop"), object: nil)
            player.playOnLine(EWEUrl.File_URL + url)
        }
    }
    
    @objc func CellPlayerStop(_ sender: Notification) {
        if player.state != .stopped {
            player?.stop()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension YblbCell: LGFAudioPlayerDelegate {
    func LGFAudioPlayerState(_ player: STKAudioPlayer) {
//        allTime = self.player.getAllTime()
    }
    
    func LGFAudioPlayerRun(_ player: STKAudioPlayer) {
        if player.duration > 0 {
            play_line.progress = Float(player.progress / player.duration)
            play_time.text = self.player.getTime() + "/" + allTime
        }
    }
    
    func LGFAudioPlayerStop(_ player: STKAudioPlayer) {
        play_button.lgf_ViewName = "暂停"
        play_button.setBackgroundImage(#imageLiteral(resourceName: "播放@1.5x"), for: .normal)
        play_line.progress = 0.0
        play_time.text = "00:00/" + allTime
    }
}
